<table cellspacing="0" class="b3333 main">
 <tbody><tr>
     <th align="center" class="b0230" style="width: 200px;">Nastavni predmet</th>
     <th align="center" class="b0230" style="width: 100px;"><?php echo image_tag('/backend.php/utils/imageText/width/20/height/140/x/6/y/5/font/13/background/gray/color/000000/text/'.'Polugodište'); ?></th>
     <th align="center" class="b0130"><?php echo image_tag('/backend.php/utils/imageText/width/20/height/140/x/6/y/5/font/13/background/gray/color/000000/text/'.'Odličih'); ?></th>
     <th align="center" class="b0130"><?php echo image_tag('/backend.php/utils/imageText/width/20/height/140/x/6/y/5/font/13/background/gray/color/000000/text/'.'Vrlodobrih'); ?></th>
     <th align="center" class="b0130"><?php echo image_tag('/backend.php/utils/imageText/width/20/height/140/x/6/y/5/font/13/background/gray/color/000000/text/'.'Dobrih'); ?></th>
     <th align="center" class="b0230"><?php echo image_tag('/backend.php/utils/imageText/width/20/height/140/x/6/y/5/font/13/background/gray/color/000000/text/'.'Dovoljnih'); ?></th>
     <th align="center" class="b0230"><?php echo image_tag('/backend.php/utils/imageText/width/20/height/140/x/6/y/5/font/13/background/gray/color/000000/text/'.'Svega pozitivnih'); ?></th>
     <th align="center" class="b0130"><?php echo image_tag('/backend.php/utils/imageText/width/20/height/140/x/6/y/5/font/13/background/gray/color/000000/text/'.'Nedovoljnih'); ?></th>
     <th align="center" class="b0130"><?php echo image_tag('/backend.php/utils/imageText/width/20/height/140/x/6/y/5/font/13/background/gray/color/000000/text/'.'Neocenjenih'); ?></th>
     <th align="center" class="b0230"><?php echo image_tag('/backend.php/utils/imageText/width/20/height/140/x/6/y/5/font/13/background/gray/color/000000/text/'.'Svega učenika'); ?></th>
     <th align="center" class="b0230"><?php echo image_tag('/backend.php/utils/imageText/width/20/height/140/x/6/y/5/font/13/background/gray/color/000000/text/'.'Srednja ocena'); ?></th>
     <th align="center" class="b0130"><?php echo image_tag('/backend.php/utils/imageText/width/20/height/140/x/6/y/5/font/13/background/gray/color/000000/text/'.'Potpis nastavnika'); ?></th>
 </tr>

 <?php
 $i = 0;
 $count = count($predmets);
 foreach($predmets as $predmet_id => $naziv)
 {
 ?>
 <tr>
  <?php if(++$i == $count){?>
  <th class="b0210 tab_left" rowspan="3"><?php echo $naziv; ?></th>
  <?php } else {?>
  <th class="b0220 tab_left" rowspan="3"><?php echo $naziv; ?></th>
  <?php }?>
  <th class="b0210 tab_left">Prvo</th>
  <?php
     $ocenas = isset($zakljucnas[$predmet_id][1]) ? array_count_values($zakljucnas[$predmet_id][1]) : array();
     if((array_sum($ocenas)))
       $prosek = ((isset($ocenas[5]) ? ($ocenas[5] * 5) : 0) + (isset($ocenas[4]) ? ($ocenas[4] * 4) : 0) + (isset($ocenas[3]) ? ($ocenas[3] * 3) : 0) + (isset($ocenas[2]) ? ($ocenas[2] * 2) : 0) + (isset($ocenas[1]) ? ($ocenas[1] * 1) : 0)) / array_sum($ocenas);
     else $prosek = 0;
  ?>
     <td align="center" class="b0110"><?php echo (isset($ocenas[5]) ? $ocenas[5] : '0') ?></td>
     <td align="center" class="b0110"><?php echo (isset($ocenas[4]) ? $ocenas[4] : '0') ?></td>
     <td align="center" class="b0110"><?php echo (isset($ocenas[3]) ? $ocenas[3] : '0') ?></td>
     <td align="center" class="b0210"><?php echo (isset($ocenas[2]) ? $ocenas[2] : '0') ?></td>
     <td align="center" class="b0210"><?php echo (isset($ocenas[1]) ? (array_sum($ocenas) -  $ocenas[1]) : array_sum($ocenas)) ?></td>
     <td align="center" class="b0110"><?php echo (isset($ocenas[1]) ? $ocenas[1] : '0') ?></td>
     <td align="center" class="b0110"><?php echo $uceniks - array_sum($ocenas) ?></td>
     <td align="center" class="b0210"><?php echo $uceniks ?></td>
     <td align="center" class="b0210"><?php echo number_format($prosek, 2, ',', '.') ?></td>
     <?php if($i == $count){?>
     <td align="center" class="b0110" rowspan="3"></td>
     <?php } else {?>
     <td align="center" class="b0120" rowspan="3"></td>
     <?php }?>
  </tr>

  <tr>
     <th class="b0210 tab_left">Drugo</th>
  <?php
     $ocenas = isset($zakljucnas[$predmet_id][2]) ? array_count_values($zakljucnas[$predmet_id][2]) : array();
     if((array_sum($ocenas)))
       $prosek = ((isset($ocenas[5]) ? ($ocenas[5] * 5) : 0) + (isset($ocenas[4]) ? ($ocenas[4] * 4) : 0) + (isset($ocenas[3]) ? ($ocenas[3] * 3) : 0) + (isset($ocenas[2]) ? ($ocenas[2] * 2) : 0) + (isset($ocenas[1]) ? ($ocenas[1] * 1) : 0)) / array_sum($ocenas);
     else $prosek = 0;
  ?>
     <td align="center" class="b0110"><?php echo (isset($ocenas[5]) ? $ocenas[5] : '0') ?></td>
     <td align="center" class="b0110"><?php echo (isset($ocenas[4]) ? $ocenas[4] : '0') ?></td>
     <td align="center" class="b0110"><?php echo (isset($ocenas[3]) ? $ocenas[3] : '0') ?></td>
     <td align="center" class="b0210"><?php echo (isset($ocenas[2]) ? $ocenas[2] : '0') ?></td>
     <td align="center" class="b0210"><?php echo (isset($ocenas[1]) ? (array_sum($ocenas) -  $ocenas[1]) : array_sum($ocenas)) ?></td>
     <td align="center" class="b0110"><?php echo (isset($ocenas[1]) ? $ocenas[1] : '0') ?></td>
     <td align="center" class="b0110"><?php echo $uceniks - array_sum($ocenas) ?></td>
     <td align="center" class="b0210"><?php echo $uceniks ?></td>
     <td align="center" class="b0210"><?php echo number_format($prosek, 2, ',', '.') ?></td>
 </tr>

   <?php if($i == $count){?>
   <tr>
     <th class="b0210 tab_left">31. Avgust</th>

     <td align="center" class="b0110"></td>
     <td align="center" class="b0110"></td>
     <td align="center" class="b0110"></td>
     <td align="center" class="b0210"></td>
     <td align="center" class="b0210"></td>
     <td align="center" class="b0110"></td>
     <td align="center" class="b0110"></td>
     <td align="center" class="b0210"></td>
     <td align="center" class="b0210"></td>
 </tr>
<?php }
 else {?>
   <tr>
     <th align="center" class="b0220">31. Avgust</th>

     <td align="center" class="b0120"></td>
     <td align="center" class="b0120"></td>
     <td align="center" class="b0120"></td>
     <td align="center" class="b0220"></td>
     <td align="center" class="b0220"></td>
     <td align="center" class="b0120"></td>
     <td align="center" class="b0120"></td>
     <td align="center" class="b0220"></td>
     <td align="center" class="b0220"></td>
 </tr>
<?php }?>
<?php }?>

</tbody></table>